//
//  SPModifyPasswordViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/4/12.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPModifyPasswordViewController.h"
#import "UIColor+CreateMethod.h"
#import "SPHTTPRequestOperationManager.h"
#import "Methods.h"

@interface SPModifyPasswordViewController(){
    UITextField * m_oldPasswordField;
    UITextField * m_passwordField;
    UITextField * m_confirmPasswordField;
    UITextField * m_verificationCodeField;
    UIButton * m_sendVerificationCodeButton;
    NSString * m_verificationCode;
    float leftoffset;
}
@end

@implementation SPModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    leftoffset = 50;
    
    m_verificationCode = @"initial_code";
    
    m_oldPasswordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, 50, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_oldPasswordField setBackgroundColor:[UIColor clearColor]];
    [m_oldPasswordField setPlaceholder:@"旧密码"];
    [self.view addSubview:m_oldPasswordField];
    
    UIImageView * pwIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_oldPasswordField.frame.origin.y, 20, 20)];
    [pwIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:pwIcon];
    
    m_passwordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, m_oldPasswordField.frame.origin.y + m_oldPasswordField.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_passwordField setBackgroundColor:[UIColor clearColor]];
    [m_passwordField setPlaceholder:@"新密码"];
    [self.view addSubview:m_passwordField];
    
    UIImageView * usernameIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_passwordField.frame.origin.y, 20, 20)];
    [usernameIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:usernameIcon];
    
    m_confirmPasswordField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, m_passwordField.frame.origin.y + m_passwordField.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_confirmPasswordField setBackgroundColor:[UIColor clearColor]];
    [m_confirmPasswordField setPlaceholder:@"确认密码"];
    [self.view addSubview:m_confirmPasswordField];
    
    UIImageView * paswordIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_confirmPasswordField.frame.origin.y, 20, 20)];
    [paswordIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:paswordIcon];
    
    m_verificationCodeField = [[UITextField alloc]initWithFrame:CGRectMake(leftoffset, m_confirmPasswordField.frame.origin.y + m_confirmPasswordField.frame.size.height + 20, self.view.frame.size.width - 2*leftoffset, 30)];
    [m_verificationCodeField setBackgroundColor:[UIColor clearColor]];
    [m_verificationCodeField setPlaceholder:@"验证码"];
    [self.view addSubview:m_verificationCodeField];
    
    UIImageView * vcodeIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, m_verificationCodeField.frame.origin.y, 20, 20)];
    [vcodeIcon setImage:[UIImage imageNamed:@"login_password_icon"]];
    [self.view addSubview:vcodeIcon];
    
    m_sendVerificationCodeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width -70- leftoffset, m_verificationCodeField.frame.origin.y, 100, 30)];
    [m_sendVerificationCodeButton setBackgroundImage:[UIImage imageNamed:@"veri_code_btn"] forState:UIControlStateNormal];
    [m_sendVerificationCodeButton setTitle:@"获取短信验证码" forState:UIControlStateNormal];
    m_sendVerificationCodeButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [m_sendVerificationCodeButton addTarget:self action:@selector(sendVerificationCodeButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m_sendVerificationCodeButton];
    
    UIButton * nextButton = [[UIButton alloc]initWithFrame:CGRectMake(0, m_verificationCodeField.frame.origin.y + m_verificationCodeField.frame.size.height + 10, self.view.frame.size.width, 40)];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"login_login_btn"] forState:UIControlStateNormal];
    [nextButton setTitle:@"确认" forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(nextButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, nextButton.frame.origin.y + nextButton.frame.size.height + 30, self.view.frame.size.width, 40)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor colorWith8BitRed:122 green:177 blue:226 alpha:1] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonListener) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignTextField)];
    
    tapBg.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapBg];
}

- (void)sendVerificationCodeButtonListener{
    if ([Methods isExistenceNetwork]) {
        [self verificationCode];
        [m_sendVerificationCodeButton setEnabled:NO];    }
    else{
        [self showHintProgressWithText:@"无可用网络"];
    }
}

- (void)backButtonListener{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)resignTextField{
    [m_oldPasswordField resignFirstResponder];
    [m_passwordField resignFirstResponder];
    [m_confirmPasswordField resignFirstResponder];
    [m_verificationCodeField resignFirstResponder];
}

- (void)nextButtonListener{
    if (m_oldPasswordField.text.length == 0) {
        [self showHintProgressWithText:@"请输入旧密码"];
    }
    else if (![m_oldPasswordField.text isEqualToString:PASSWORD]){
        [self showHintProgressWithText:@"旧密码输入错误"];
    }
    else if (m_passwordField.text.length == 0) {
        [self showHintProgressWithText:@"请输入新密码"];
    }
    else if (m_confirmPasswordField.text.length == 0){
        [self showHintProgressWithText:@"请输入确认密码"];
    }
    else if (![m_passwordField.text isEqualToString:m_confirmPasswordField.text]){
        [self showHintProgressWithText:@"两次输入密码不一致"];
    }
    else if (m_verificationCodeField.text.length == 0){
        [self showHintProgressWithText:@"请输入验证码"];
    }
    else if (![m_verificationCodeField.text isEqualToString:m_verificationCode]){
        [self showHintProgressWithText:@"验证码错误"];
    }
    else{
        if ([Methods isExistenceNetwork]) {
            [self resetPassword];
        }
        else{
            [self showHintProgressWithText:@"无可用网络"];
        }
    }
}

- (void)verificationCode{
    NSDictionary * para = [[NSDictionary alloc]initWithObjectsAndKeys:PHONE,@"phone", nil];
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    [manager sendVerificationCodePara:para Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"send_verification_code>>>>>>>>SUCCESS: %@\nmessage:%@",responseObject,[responseObject valueForKey:@"message"]);
        
        if ([[responseObject valueForKey:@"is_success"] intValue] == 1) {
            [self showHintProgressWithText:@"短信验证码申请已发送"];
            m_verificationCode = [NSString stringWithString:[responseObject valueForKey:@"phone_code"]];
        }
        else{
            [self showHintProgressWithText:[responseObject valueForKey:@"message"]];
            [m_sendVerificationCodeButton setEnabled:YES];
        }
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"send_verification_code>>>>>>>>Error: %@", error);
        [self closeProgress];
        [self showHintProgressWithText:@"邀请码发送失败"];
        [m_sendVerificationCodeButton setEnabled:YES];
    }];
}

- (void)resetPassword{
    [self showProgressAutoCloseInNetwork:@"重设密码中"];
    NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:m_verificationCodeField.text,@"phone_code",PHONE,@"phone",[Methods md5:m_passwordField.text],@"newpwd", nil];
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    [manager resetPasswordPara:dic Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"reset_password>>>>>>>>SUCCESS: %@\nmessage:%@",responseObject,[responseObject valueForKey:@"message"]);
        
        if ([[responseObject valueForKey:@"is_success"] intValue] != 0) {
            [self showHintProgressWithText:@"密码修改成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else{
            [self closeProgress];
            [self showHintProgressWithText:[responseObject valueForKey:@"message"]];
        }
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"reset_password>>>>>>>>Error: %@", error);
        [self closeProgress];
        [self showHintProgressWithText:@"密码设置失败"];
    }];
}


@end
